package day16;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

/**
 * 需求： 对学生的年龄进行排序 因为数据是以键值对存在的， 因此要使用可以排序的TreeMap集合
 * 
 * @author wangbiao
 * 
 */
public class MapSortTest {

	public static void main(String[] args) {

		Map<Student, String> map = new TreeMap<Student, String>(new StudentAgeComparator());
		map.put(new Student("wangwu", 21), "shanghai");
		map.put(new Student("wangbiao", 25), "shanghai");
		map.put(new Student("wangwu", 23), "beijing");
		map.put(new Student("wangxiaoer", 21), "chengdu");
		map.put(new Student("wangsan", 20), "shanghai");
		Set<Entry<Student, String>> entrySet = map.entrySet();
		for (Iterator<Entry<Student, String>> iterator = entrySet.iterator(); iterator
				.hasNext();) {
			Entry<Student, String> entry = iterator.next();
			Student key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key + "----->" +value);
		}
	}
}
class StudentAgeComparator implements Comparator<Student> {

	@Override
	public int compare(Student stu1, Student stu2) {
		int num = stu1.getAge() - stu2.getAge();
		if (num == 0) {
			return stu1.getName().compareTo(stu2.getName());
		}
		return num;
	}
	
}
